package org.ling.xuan.rule;

import cn.hutool.core.io.FileUtil;
import org.ling.xuan.config.SysConstant;
import org.ling.xuan.config.SystemProperties;
import org.ling.xuan.context.CheckChannelContext;
import org.ling.xuan.context.CheckPipelineContext;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;

public class JavaFileCheckRule extends CheckRule<CheckChannelContext, CheckPipelineContext>{
    @Override
    public void check(CheckPipelineContext pipeContext, CheckChannelContext channelContext) {
        String filePath = pipeContext.getFilePath();
        List<String> ss = FileUtil.readLines(filePath, StandardCharsets.UTF_8).stream().filter(s->s.trim().startsWith("import ")).filter(s->s.trim().endsWith(";")).collect(Collectors.toList());
        for(String s:ss){
            if(s.trim().contains("*")){
                pipeContext.error("导入包不允许包含*"+s);
            }
        }
        boolean f = SystemProperties.isTrueDefaultTrue(SysConstant.checkJavaBankImport);
        if(f) {
            if (!filePath.contains("bank")) {
                for (String s : ss) {
                    if (s.trim().contains(".bank.") || s.trim().endsWith(".bank;")) {
                        pipeContext.error("非个性化文件不允许导入个性化包:" + s);
                    }
                }
            }
        }
    }

    @Override
    public boolean match(CheckPipelineContext pipeContext, CheckChannelContext channelContext) {
        return pipeContext.getFilePath().endsWith(".java");
    }
    
}
